package org.example.ymcc.consumer;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.example.ymcc.constant.Constants;
import org.example.ymcc.constant.MQConstants;
import org.example.ymcc.domain.CourseCollect;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

@Component
@RocketMQMessageListener(consumerGroup = "PhoneConsumer",
        topic = MQConstants.COURSE_ON_LINE_TOPIC,
        selectorExpression = MQConstants.COURSE_ON_LINE_TAG
)
@Slf4j
public class PhoneConsumer implements RocketMQListener<MessageExt> {

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String jsonMessage = new String(body, Constants.UTF8);
            List<CourseCollect> courseCollects = JSONObject.parseArray(jsonMessage, CourseCollect.class);

            //发起远程调用 获取手机号
            for (CourseCollect collect : courseCollects) {
                //调用 短信验证码的工具 发送短信
                System.out.println("用户Id是:"+collect.getUserId()+"关注的"+collect.getCourseId()+"的课程一上线");
            }

        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
